//
//  MeasureCircle.swift
//  foreign_swift
//
//  Created by 黄敦仁 on 15/5/4.
//  Copyright (c) 2015年 hdr. All rights reserved.
//

import UIKit
import GLKit

let PI:CGFloat = 3.1415926535898
let HALF_PI:CGFloat = -PI/2

class MeasureCircle:UIView {
    
    var viewWidth:CGFloat
    var circleWidth:CGFloat = 15
    
    var innerCircleRadius:CGFloat = 60
    
    var weightScore:CGFloat = 90
    var waterScore:CGFloat = 63
    var bodyfatScore:CGFloat = 84
    
    var _waterScore:CGFloat = 0
    var _weightScore:CGFloat = 0
    var _bodyfatScore:CGFloat = 0
    var animationTimer:NSTimer!
    
    let innerColor:UIColor
    let middelColor:UIColor
    let outerColoer:UIColor
    
    init(){
        innerCircleRadius = Const.screen_width / 5
        viewWidth = (innerCircleRadius + circleWidth * 2)*2 + circleWidth
        innerColor = UIColor.colorWithHexString("#68C4F0")
        middelColor = UIColor.colorWithHexString("#EDC855")
        outerColoer = UIColor.colorWithHexString("#D46795")
        super.init(frame: CGRect(x: 0, y: 0, width: viewWidth, height: viewWidth))
        
        self.center = CGPoint(x: Const.screen_width_center, y: viewWidth / 2 + 10)
        
        self.backgroundColor = UIColor.clearColor()

    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func drawRect(rect: CGRect) {
        super.drawRect(rect)

        let context = UIGraphicsGetCurrentContext()
        
        drawCircle(context, x: viewWidth/2, y: self.bounds.height/2, radius: innerCircleRadius,percent:_weightScore,width:circleWidth,color:innerColor)
        
        drawCircle(context, x: self.bounds.width/2, y: self.bounds.height/2, radius: innerCircleRadius + circleWidth,percent:_waterScore,width:circleWidth,color:middelColor)
        
        drawCircle(context, x: self.bounds.width/2, y: self.bounds.height/2, radius: innerCircleRadius + circleWidth*2,percent:_bodyfatScore,width:circleWidth,color:outerColoer)
    }
    
    func drawCircle(context:CGContext,x:CGFloat,y:CGFloat,radius:CGFloat,percent:CGFloat,width:CGFloat=5,startAngle:CGFloat=HALF_PI,color:UIColor){

        let red = UnsafeMutablePointer<CGFloat>.alloc(1)

        let green = UnsafeMutablePointer<CGFloat>.alloc(1)

        let blue = UnsafeMutablePointer<CGFloat>.alloc(1)

        let alpha = UnsafeMutablePointer<CGFloat>.alloc(1)
        color.getRed(red, green: green, blue: blue, alpha: alpha)
        
        CGContextSetRGBStrokeColor(context, red.memory, green.memory, blue.memory, alpha.memory)
        
        CGContextSetLineWidth(context, width)
        CGContextBeginPath(context)

        CGContextAddArc(context, x, y, radius, startAngle, startAngle + (2*PI*percent)/100, 0)
        CGContextStrokePath(context)
    }
    
    func startAnimation(){
        _weightScore = 0
        _waterScore = 0
        _bodyfatScore = 0
        animationTimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "perAnimation", userInfo: nil, repeats: true)
        
    }
    
    func perAnimation(){
        _weightScore += 0.5
        _waterScore += 0.5
        _bodyfatScore += 0.5
        
        if _weightScore >= weightScore {
            _weightScore = weightScore
        }
        if _waterScore >= waterScore {
            _waterScore = waterScore
        }
        if _bodyfatScore >= bodyfatScore {
            _bodyfatScore = bodyfatScore
        }
        if allEquals() {
            animationTimer.invalidate()
        }
        self.setNeedsDisplay()
    }
    
    func allEquals()->Bool{
        return _weightScore==weightScore && _waterScore==waterScore && _bodyfatScore==bodyfatScore
    }
    
}
